// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.system.controller.request.QueryHierarchyReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Hierarchy;
import com.liuxinlong.modules.system.controller.request.UpdateHierarchyReq;
import com.liuxinlong.modules.system.service.HierarchyService;

/**
 * 层级关系控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月26日
 */
@RestController
@RequestMapping("/hierarchy")
public class HierarchyController {
	@Autowired
	private HierarchyService hierarchyService;

	/**
	 * 分页查询层级类型信息
	 * 
	 * @param req 请求信息
	 * @return 层级类型信息信息列表
	 */
	@PostMapping("page")
	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.HIERARCHY_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageHierarchyInfo(@RequestBody QueryHierarchyReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("name",req.getName());
		queryMap.put("parent",req.getParent());
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		List<Map<String, Object>> dataList = hierarchyService.pageHierarchyInfo(queryMap);
		int total = hierarchyService.countHierarchy(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询层级类型信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 新增层级类型信息信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增层级信息",business = LogBusinessConstants.HIERARCHY_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addHierarchyInfo(@RequestBody UpdateHierarchyReq req) {
		Hierarchy hierarchy = new Hierarchy();
		hierarchy.setName(req.getName());
		hierarchy.setActionScope(req.getActionScope());
		hierarchy.setIsGeneral(req.getIsGeneral());
		hierarchy.setParentId(req.getParentId());
		hierarchy.setRemark(req.getRemark());
		hierarchyService.addHierarchyInfo(hierarchy);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增层级类型信息信息成功");
		return resp;
	}

	/**
	 * 修改层级类型信息信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改层级信息",business = LogBusinessConstants.HIERARCHY_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateHierarchyInfo(@RequestBody UpdateHierarchyReq req) {
		Hierarchy hierarchy = new Hierarchy();
		hierarchy.setId(req.getId());
		hierarchy.setName(req.getName());
		hierarchy.setActionScope(req.getActionScope());
		hierarchy.setIsGeneral(req.getIsGeneral());
		hierarchy.setParentId(req.getParentId());
		hierarchy.setRemark(req.getRemark());
		hierarchyService.updateHierarchyInfo(hierarchy);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改层级类型信息信息成功");
		return resp;
	}

	/**
	 * 删除层级类型信息信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除层级信息",business = LogBusinessConstants.HIERARCHY_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteHierarchyInfo(@RequestBody BaseDeleteReq req) {
		hierarchyService.deleteHierarchyInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除层级类型信息信息成功");
		return resp;
	}

	/**
	 * 树模糊查询层级信息
	 *
	 * @param req 请求信息
	 * @return 层级展开列表
	 */
	@PostMapping("tree-query")
	@LogAnnotation(value ="树模糊查询层级信息",business = LogBusinessConstants.HIERARCHY_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp treeQueryHierarchy(@RequestBody QueryHierarchyReq req) {
		List<String> hierarchyList = hierarchyService.treeQueryHierarchy(req.getName());
		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("expandedKeys",hierarchyList);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("树模糊查询层级信息成功");
		resp.setDataMap(dataMap);
		return resp;
	}

}
